<template>
  <div>
    <el-tree ref="tree" node-key="id" :data="list" :props="{label:'name'}" show-checkbox default-expand-all check-strictly />

    <el-row>
      <el-button type="primary" size="small" @click="hSubmit">确认</el-button>
      <el-button type="primary" size="small">取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { transDataToTreeData } from '@/utils/index'
import { getRolesId, assignPrem } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      // 获取权限列表
      const res = await getPermissionList()
      console.log(res, 'getPermissionList')
      this.list = transDataToTreeData(res.data)

      // 获取用某个角色的信息
      const detail = await getRolesId(this.curId)
      console.log(detail, 'getRolesId')
      // 被勾选的复选框  setCheckedKeys 设置目前勾选的(res1.data.permIds)节点
      this.$refs.tree.setCheckedKeys(detail.data.permIds)
    },
    async hSubmit() {
      // 给角色分配权限
      // getCheckedKeys() 返回复选框勾选的节点
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPrem({ id: this.curId, permIds: permIds })
      this.$emit('success')
    }
  }
}
</script>
